package com.atguigu.staservice.service.impl;

import com.atguigu.commonutils.R;
import com.atguigu.staservice.client.UcenterClient;
import com.atguigu.staservice.entity.StatisticsDaily;
import com.atguigu.staservice.mapper.StatisticsDailyMapper;
import com.atguigu.staservice.service.StatisticsDailyService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import net.bytebuddy.description.field.FieldDescription;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 网站统计日数据 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2021-04-20
 */
@Service
public class StatisticsDailyServiceImpl extends ServiceImpl<StatisticsDailyMapper, StatisticsDaily> implements StatisticsDailyService {

    @Autowired
    private UcenterClient ucenterClient;
    //生成统计数据
    @Override
    public void createStaDaily(String day) {
        //1 删除要统计那天的数据
        QueryWrapper<StatisticsDaily> staWrapper = new QueryWrapper<>();
        staWrapper.eq("date_calculated",day);
        baseMapper.delete(staWrapper);

        //2 跨模块调用获取数据
        R r = ucenterClient.registerCount(day);
        Integer registerNum = (Integer) r.getData().get("countRegister");
        //3 别的指标，模拟生成
        Integer loginNum = RandomUtils.nextInt(100, 200);//TODO
        Integer videoViewNum = RandomUtils.nextInt(100, 200);//TODO
        Integer courseNum = RandomUtils.nextInt(100, 200);//TODO

        //4 封装对象，插入数据库
        StatisticsDaily daily = new StatisticsDaily();
        daily.setRegisterNum(registerNum);
        daily.setLoginNum(loginNum);
        daily.setVideoViewNum(videoViewNum);
        daily.setCourseNum(courseNum);
        daily.setDateCalculated(day);

        baseMapper.insert(daily);

    }

    //根据条件查询统计数据
    @Override
    public Map<String, Object> showChart(String begin, String end, String type) {
        //1 根据条件查询数据
        QueryWrapper<StatisticsDaily> wrapper = new QueryWrapper<>();
        wrapper.between("date_calculated",begin,end);
        wrapper.select("date_calculated",type);
        List<StatisticsDaily> dailyList = baseMapper.selectList(wrapper);

        //2 遍历集合进行封装x轴数据（统计日期）
        List<String> dateCalculatedList = new ArrayList<>();//x
        List<Integer> dataList = new ArrayList<>();//y
        for (int i = 0; i < dailyList.size(); i++) {
            //2.1 取出统计数据对象
            StatisticsDaily daily = dailyList.get(i);
            dateCalculatedList.add(daily.getDateCalculated());
            //3 封装y轴数据（指标数据）
            switch (type) {
                case "register_num":
                    dataList.add(daily.getRegisterNum());
                    break;
                case "login_num":
                    dataList.add(daily.getLoginNum());
                    break;
                case "video_view_num":
                    dataList.add(daily.getVideoViewNum());
                    break;
                case "course_num":
                    dataList.add(daily.getCourseNum());
                    break;
                default:
                    break;
            }


        }
        //4 x,y轴数据存入map
        Map<String,Object> map = new HashMap<>();
        map.put("dateCalculatedList",dateCalculatedList);
        map.put("dataList",dataList);
        return map;
    }
}
